$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'goodsMonitor/goodsmonitor/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,align:'center'},
			{ label: '关联关系id', name: 'sdbId', index: 'sdb_id', width: 80,align:'center' },
			{ label: '信息记录类型', name: 'type', index: 'type', width: 80 ,align:'center'},
			{ label: '更改信息记录', name: 'message', index: 'message', width: 300 },
			// { label: '更新时间', name: 'updateTime', index: 'update_time', width: 80 },
			// { label: '操作人', name: 'updateUser', index: 'update_user', width: 80 },
			{ label: '创建人', name: 'createName', index: 'create_id', width: 80 ,align:'center'},
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 ,align:'center'}
        ],
		viewrecords: true,
        height: 700,
        rowNum: 25,
		rowList : [25,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		goodsMonitor: {},
		message:[],
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.goodsMonitor = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		// saveOrUpdate: function (event) {
		// 	var url = vm.goodsMonitor.id == null ? "goodsMonitor/goodsmonitor/save" : "goodsMonitor/goodsmonitor/update";
		// 	$.ajax({
		// 		type: "POST",
		// 	    url: baseURL + url,
         //        contentType: "application/json",
		// 	    data: JSON.stringify(vm.goodsMonitor),
		// 	    success: function(r){
		// 	    	if(r.code === 0){
		// 				alert('操作成功', function(index){
		// 					vm.reload();
		// 				});
		// 			}else{
		// 				alert(r.msg);
		// 			}
		// 		}
		// 	});
		// },
		// del: function (event) {
		// 	var ids = getSelectedRows();
		// 	if(ids == null){
		// 		return ;
		// 	}
		// 	confirm('确定要删除选中的记录？', function(){
		// 		$.ajax({
		// 			type: "POST",
		// 		    url: baseURL + "goodsMonitor/goodsmonitor/delete",
         //            contentType: "application/json",
		// 		    data: JSON.stringify(ids),
		// 		    success: function(r){
		// 				if(r.code == 0){
		// 					alert('操作成功', function(index){
		// 						$("#jqGrid").trigger("reloadGrid");
		// 					});
		// 				}else{
		// 					alert(r.msg);
		// 				}
		// 			}
		// 		});
		// 	});
		// },
		getInfo: function(id){
			$.get(baseURL + "goodsMonitor/goodsmonitor/info/"+id, function(r){
                vm.goodsMonitor = r.goodsMonitor;
                //处理goodsMonitor数据
                // vm.handlerDate();
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
		//处理数据以供展示清晰
        handlerDate:function(){
            var message = JSON.parse(vm.goodsMonitor.message);
			for(var i = 0;i<message.length;i++){
				var msg = {};
				var item=message[i];
				var key= Object.keys(item);
				var value = item[key];
				var split = key.split(":");
				msg.type = split[0];
                var split1 = split[1].split("-");
                msg.key = split1[0];
                msg.field = split1[1];
                var split2 = value.split("->");
                msg.oldValue = split2[0];
                msg.newValue = split2[1];
				vm.message.push(msg);
			}
			console.info(vm.message);
		}
	}
});